<?php


namespace App\Http\Controllers\Web;


use App\Http\Controllers\Controller;
use App\Http\Repositories\Services\IBrand;
use App\Http\Repositories\Services\IProduct;
use App\Util\Helper;
use App\Util\ResultsVo;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Config;

class HomeController extends Controller
{
    protected $brandService;
    protected $productService;
    public function __construct(
        IBrand $brandService,
        IProduct $productService
    )
    {
        $this->brandService = $brandService;
        $this->productService = $productService;

    }


    public function index()
    {
        return view('web.index');
    }

    public function home(Request $request)
    {
        $type = intval($request->get('type'));
        $model = Config::get('constant.product_model');
        if ($type) {
            $model = array_except($model, [1,2,3,4]);
        } else {
            $model = array_except($model, [5,6,7]);
        }
        $brand = $this->brandService->getParentId(0);
        $data = [];
        $data['product_model'] = Helper::dataToKey($model);
        $data['year'] = Helper::dataToKey(Config::get('constant.year'));
        $data['brand'] = Helper::arrayField($brand,['id','name']);
        return response()->json(new ResultsVo(['data' => $data]));
    }


    public function brand($id)
    {
        $brand = $this->brandService->getParentId($id);
        return response()->json(new ResultsVo(['data' => $brand]));
    }


    public function product(Request $request)
    {
        $search = $request->only(['brand_id','brand_model_id','product_model']);
        $data = $this->productService->byProduct($search);
        return response()->json(new ResultsVo(['data' => $data]));
    }
}